package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;

import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        PageResult page = roleService.findPage(pageBean);
        if (page != null) {
            return page;
        }
        return null;
    }

    //添加角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.add(permissionIds, menuIds, role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_ROLE_FAIL);
    }

    //删除角色
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
    }

    //查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleService.findById(id);
        if (role != null) {
            return new Result(true,"",role);
        }
        return null;
    }


    //查询
    @RequestMapping("/findPermissionIdByRoleId")
    public Result findPermissionIdByRoleId(Integer roleId){
        List<Integer> permissionIds= roleService.findPermissionIdByRoleId(roleId);
        if (permissionIds != null) {
            return new Result(true,"",permissionIds);
        }
        return new Result(false,"",permissionIds);
    }


    //查询
    @RequestMapping("/findMenuIdByRoleId")
    public Result findMenuIdByRoleId(Integer roleId){
        List<Integer> menuId = roleService.findMenuIdByRoleId(roleId);
        if (menuId != null) {
            return new Result(true,"",menuId);
        }
        return null;
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
    }
}
